<@html>
<script type="text/javascript" src="${path}/js/dojo/dojo/inputForm.gzjs"></script>
<script type="text/javascript" src="${path}/js/dojo/dojo/checkboxTree.gzjs"></script>
<script type="text/javascript">
	//	dojo.require("dijit.dijit");
	//	dojo.require("dijit.form.Form");
	//	dojo.require("dijit.form.ValidationTextBox");
	//	dojo.require("dijit.form.SimpleTextarea");
	//	dojo.require("dijit.Tree");
	//  dojo.require("dojo.data.ItemFileWriteStore");
	//	dojo.require("dijit.tree.ForestStoreModel");
	//	dojo.require("dijit.Menu");
	//	dojo.require("dijit.form.Button");
	//	dojo.require("dijit.layout.ContentPane");
</script>
<div dojoType="dijit.tree.ForestStoreModel" jsId="model" childrenAttrs="subMenus"
     store="store" rootLabel="菜单列表">
</div>
<div>
	<div dojoType="dijit.form.Button" onclick="addMenu" iconClass="file_add">增加菜单</div>
	<div dojoType="dijit.form.Button" onclick="editMenu" iconClass="file_edit">修改菜单</div>
	<div dojoType="dijit.form.Button" onclick="deleteMenu" iconClass="file_delete">删除菜单</div>
</div>
<div dojoType="dijit.Tree" style="width:300px;margin-left:5px;" id="menuTree"
     jsId="menuTree" model="model" openOnClick="false" showRoot="true">
</div>
<div dojoType="dijit.layout.ContentPane" jsId="detail"
     style="position:absolute;left:230px;top:2px;width:600px;height:90%">
</div>

<#--<div dojoType="dijit.Menu" id="menu" targetNodeIds="menuTree" style="display: none;">-->
<#--<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="addMenu" iconClass="file_add">-->
<#--&nbsp;增加子菜单&nbsp;-->
<#--</div>-->
<#--<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="editMenu" iconClass="file_edit">-->
<#--修改菜单信息-->
<#--</div>-->
<#--<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="deleteMenu" iconClass="file_delete">-->
<#--&nbsp;删除菜单&nbsp;-->
<#--</div>-->
<#--</div>-->
</@html>
<script type="text/javascript">
	var store = new dojo.data.ItemFileWriteStore({
				data:{
					identifier:"id",
					label:"name",
					items:${menuTreeStore!"[]"}
				}
			})
	dojo.ready(function() {
		menuTree.getIconClass = function(item, opened) {
			if (store.isItem(item)) {
				return store.getValue(item, "icon");
			}
			return null;
		}
	})

	function btnAddClick() {
		if (dijit.byId("detailForm").validate()) {
			dojo.xhrPost({
						url:"${path}/system/menu.add.do",
						form:dojo.byId("detailForm"),
						handleAs:"json",
						load:function(json) {
							parent.Info("保存成功！");
							var selected = menuTree.get("selectedItem");
							model.newItem({"id":json.id,"name":json.name,"level":json.level,"icon":json.icon}, selected, 0);
							detail.set("content", "");
						}
					})
		}
	}

	function btnSaveClick() {
		if (dijit.byId("detailForm").validate()) {
			dojo.xhrPost({
						url:"${path}/system/menu.save.do",
						form:dojo.byId("detailForm"),
						handleAs:"json",
						load:function(json) {
							if (json.state === "success") {
								parent.Info(json.message);
								var selected = menuTree.get("selectedItem");
								store.setValue(selected, "name", json.name);
								store.setValue(selected, "level", json.level);
								store.setValue(selected, "icon", json.icon);
								detail.set("content", "");
							} else if (json.state === "error") {
								parent.Error(json.message);
							}
						}
					})
		}
	}

	function btnCloseClick() {
		detail.set("content", "");
	}

	function editMenu() {
		var selected = menuTree.get("selectedItem");
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			detail.set("href", "${path}/system/menu.load.do?processType=update&menu.id=" + id);
		}
	}

	/**
	 * 新增子菜单
	 */
	function addMenu(e) {
		var selected = menuTree.get("selectedItem");
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			detail.set("href", "${path}/system/menu.load.do?processType=add&pid=" + id);
		}
	}

	function deleteMenu(ingoreSubOrg) {
		var selected = menuTree.get("selectedItem");
		var ingore = ingoreSubOrg || "0";
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			dojo.xhrGet({
						url:"${path}/system/menu.delete.do?menu.id=" + id + "&ingore=" + ingore,
						handleAs:"json",
						load:function(message) {
							var type = 0;
							var msg = [];
							for (var i = 0; i < message.length; i++) {
								msg.push(message[i].text);
								//提示消息
								if (message[i].type == 1) {
									type = 1;
								}
								//警告消息
								if (message[i].type == 2) {
									type = 2;
								}
								//错误消息
								if (message[i].type == 3) {
									type = 3;
								}
							}
							if (type == 1) {
								parent.Info(msg.join("\n"), function() {
									store.deleteItem(selected);
								});
							}
							if (type == 2) {
								parent.Confirm({
											msg:msg.join("\n"),
											okCall:function() {
												deleteMenu(1);
											}
										});
							}
							if (type == 3) {
								parent.Error(msg.join("\n"));
							}
						}
					})
		}
	}
</script>